package com.gf.snoopy.analyser;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.gf.snoopy.constants.SnoopyConstants;

import freemarker.cache.TemplateLoader;

@Service
public class ContentTemplateLoader implements TemplateLoader{

	private Logger logger = Logger.getLogger(ContentTemplateLoader.class);
	
	@Override
	public Object findTemplateSource(String content) throws IOException {
		if(content.contains(SnoopyConstants.LOCAL_KEY)){
			return null;
		}
		//解决linux系统会把首个"/"干掉的问题
		if(content.contains("/")){
			content = "/"+content;
		}
		return content;
	}


	@Override
	public Reader getReader(Object templateSource, String encoding) throws IOException {
        if (!(templateSource instanceof String)) {
            throw new IllegalArgumentException(
                    "templateSource is a: " +
                            templateSource.getClass().getName());
        }
        logger.info("---templateSource:----"+templateSource);
        InputStreamReader reader = new InputStreamReader(new FileInputStream((String)templateSource),"utf-8");
		return reader;
	}
	
	@Override
	public void closeTemplateSource(Object arg0) throws IOException {	
		
	}
	
	@Override
	public long getLastModified(Object arg0) {
		return 0;
	}
}
